/*
 *      Add.java
 *
 *      Copyright 2008 Pablo Nicolas Diaz Bilotto <pablonicolas.diaz@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
package ilanguage.iltokens.operations.arimethic;

import i386CodeGenerator.RegisterAssigner;
import ilanguage.iltokens.IL_SSA;
import ilanguage.iltokens.operations.Operations;
import ilanguage.iltokens.operations.TypeSafeTwoOperandIL_SSA;
import lexer.EntryType;

/**
 * The Class Add.
 */
public class Add extends TypeSafeTwoOperandIL_SSA {

	/**
	 * Instantiates a new adds the.
	 *
	 * @param firstToken the first token
	 * @param secToken the sec token
	 */
	public Add(final IL_SSA firstToken, final IL_SSA secToken) {
		super(firstToken, secToken);
	}

	/**
	 * (non-Javadoc)
	 *
	 * @see ilanguage.iltokens.SequenciableToken#GenerateCodeMachine()
	 */
	public String GenerateCode() {
		 String firstArg = this.getFirstArg().getResultRegister();
		 String secondArg = this.getSecondArg().getResultRegister();
		 String codigo="";
		 codigo+="\t; Comienzo de Suma\n";

	     if (this.solveTypes() == EntryType.INT)
	    	 codigo+=addINTGenerateCode(firstArg,secondArg);
	     else
	    	 codigo+=addDOUBLEGenerateCode(firstArg,secondArg);
	     codigo+="\t; Fin de Suma\n\n";
	     return codigo;
	}



	private String addINTGenerateCode(String firstArg, String secondArg) {

		String codigo="";
	if (RegisterAssigner.getInstance().esRegistro(firstArg)){
   		 codigo+="\t\tadd "+firstArg+ " , "+ secondArg+"\n";
   		 RegisterAssigner.getInstance().liberarRegistro(secondArg);
   		 this.setResultRegister(firstArg);
		}
   	 else if (RegisterAssigner.getInstance().esRegistro(secondArg)){
   		 codigo+="\t\tadd "+secondArg+ " , "+ firstArg+"\n";
   		 RegisterAssigner.getInstance().liberarRegistro(firstArg);
    	 this.setResultRegister(secondArg);
   	 }
   	 else { //si ninguno de los dos es registro
   		 	String regAux = RegisterAssigner.getInstance().getRegistro();
   		 	codigo+="\t\tmov "+regAux+ " , "+ firstArg+"\n";
   		 	codigo+="\t\tadd "+regAux+ " , "+ secondArg+"\n";
   		 	this.setResultRegister(regAux);
   		    RegisterAssigner.getInstance().liberarRegistro(firstArg);
   		    RegisterAssigner.getInstance().liberarRegistro(secondArg);
   	 	  }
	codigo+=verificarOverFlowINT();
/*	codigo+=verificarUnderFlowINT();*/
	return codigo;
	}

	private String addDOUBLEGenerateCode(String first, String second) {
		String codigo="";

		if (first !="FPU"){
				codigo+="\t\tfld "+first+"\n";
		}
		if (second!="FPU"){
			codigo+="\t\tfld "+second+"\n";
		}

		codigo+="\t\tfadd\n";
		this.setResultRegister("FPU");
		codigo+=verificarUnderFlowDOUBLE();
		/*codigo+=verificarOverFlowDOUBLE();*/
		return codigo;
	}
	/**
	 * (non-Javadoc)
	 *
	 * @see ilanguage.iltokens.operations.OperationToken#getOperationName()
	 */
	public String getOperationName() {
		return Operations.ADD.toString();
	}

}